package com.ruoyi.management.statistical.carservicemsg.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.management.statistical.carservicemsg.domain.TCarServiceMsg;
import com.ruoyi.management.statistical.carservicemsg.mapper.TCarServiceMsgMapper;
import com.ruoyi.management.statistical.carservicemsg.service.ITCarServiceMsgService;
import com.ruoyi.management.statistical.carservicemsg.vo.CarEveListVo;
import com.ruoyi.management.statistical.carservicemsg.vo.CarServiceListVo;
import com.ruoyi.management.statistical.carservicemsg.vo.GetCarServiceMsgVo;
import com.ruoyi.management.statistical.carservicemsg.vo.GetPassengersNumberVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆行驶信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class TCarServiceMsgServiceImpl implements ITCarServiceMsgService {
    @Resource
    private TCarServiceMsgMapper tCarServiceMsgMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetCarServiceMsgVo getKilometerUtilization(Long companyId) {
        GetCarServiceMsgVo vo = tCarServiceMsgMapper.getKilometerUtilization(companyId);
        //行驶里程和
        BigDecimal mileages = vo.getMileages();
        //营运里程和
        BigDecimal serviceMileages = vo.getServiceMileages();
        if (null == serviceMileages)
        {
            vo.setMileRate("0.00");
        }else {
            String result = serviceMileages.multiply(new BigDecimal(100)).divide(mileages, 2,BigDecimal.ROUND_HALF_UP).toString();
            vo.setMileRate(result);
        }

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetPassengersNumberVo getPassengersNumber(Long companyId) {
        GetPassengersNumberVo vo = tCarServiceMsgMapper.getPassengersNumber(companyId);
        //总载客次数
        BigDecimal counts = vo.getServiceCounts();
        //车辆数
        BigDecimal carCount = vo.getCarCount();

        if (null ==  carCount)
        {
            vo.setCarServiceCounts("0");
        }
        else {
            String result = counts.divide(carCount, 2, BigDecimal.ROUND_HALF_UP).toString();
            vo.setCarServiceCounts(result);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetPassengersNumberVo getCarAmount(Long companyId) {
        GetPassengersNumberVo vo = tCarServiceMsgMapper.getCarAmount(companyId);
        //总载客次数
        BigDecimal counts = vo.getServiceCounts();
        //总载客次数
        BigDecimal carCount = vo.getCarCount();

        if (null ==  carCount)
        {
            vo.setCarServiceCounts("0");
        }
        else {
            String result = counts.divide(carCount, 2, BigDecimal.ROUND_HALF_UP).toString();
            vo.setCarServiceCounts(result);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetPassengersNumberVo getCarMileags(Long companyId) {
        GetPassengersNumberVo vo = tCarServiceMsgMapper.getCarMileags(companyId);
        //总载客次数
        BigDecimal counts = vo.getServiceCounts();
        //总载客次数
        BigDecimal carCount = vo.getCarCount();

        if (null ==  carCount)
        {
            vo.setCarServiceCounts("0");
        }
        else {
            String result = counts.divide(carCount, 2, BigDecimal.ROUND_HALF_UP).toString();
            vo.setCarServiceCounts(result);
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CarServiceListVo> selectCarServiceMsgList(Long companyId) {
        List<CarServiceListVo> voList = tCarServiceMsgMapper.selectCarServiceMsgList(companyId);
        if (CollUtil.isEmpty(voList)) {
            return new ArrayList<>();
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        int num = (pageNum-1)*5+1;
        for (CarServiceListVo vo : voList) {
            vo.setSortNo(num);
            num += 1;
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CarEveListVo> getCarEveList(Long companyId) {
        List<CarEveListVo> voList = tCarServiceMsgMapper.getCarEveList(companyId);
        if (CollUtil.isEmpty(voList)) {
            return new ArrayList<>();
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        int num = (pageNum-1)*5+1;
        for (CarEveListVo vo : voList) {
            vo.setSortNo(num);
            num += 1;
        }
        return voList;
    }

    @Override
    public GetPassengersNumberVo getCarYyMileags(Long companyId) {
        GetPassengersNumberVo vo = tCarServiceMsgMapper.getCarYyMileags(companyId);
        //总载客次数
        BigDecimal counts = vo.getServiceCounts();
        //总载客次数
        BigDecimal carCount = vo.getCarCount();

        if (null ==  carCount)
        {
            vo.setCarServiceCounts("0");
        }
        else {
            String result = counts.divide(carCount, 2, BigDecimal.ROUND_HALF_UP).toString();
            vo.setCarServiceCounts(result);
        }
        return vo;
    }
}
